package com.ruoyi.news.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.WxConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.news.domain.WxLoginBody;
import com.ruoyi.news.service.IXcxNewsService;
import com.ruoyi.news.service.WxMpSysUserService;
import com.ruoyi.news.utils.WxMaConfiguration;
import com.ruoyi.system.service.ISysUserService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxMpSysUserServiceImpl implements WxMpSysUserService {


    private static Map<String, WxMaService> maServices;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public Map<String, Object> getUserNamePassword(WxLoginBody body) {
        Map<String, Object> resultMap = new HashMap<>();
        if (StringUtils.isNotBlank(body.getJsCode())) {
            try {
                JSONObject obj = wxGetOpenId(body.getJsCode());
                if (StringUtils.isBlank(body.getAppletOpenId())) {
                    body.setAppletOpenId(obj.get("openid").toString());
                }
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }
        if (StringUtils.isNotBlank(body.getAppletOpenId())) {
            SysUser sysUser = new SysUser();
            sysUser.setOpenId(body.getAppletOpenId());
            SysUser sysUserWxMpList = sysUserService.selectUserByOpenId(body.getAppletOpenId());
            if ( null != sysUserWxMpList) {
                resultMap.put("userId", sysUserWxMpList.getUserId());
                resultMap.put("deptId", sysUserWxMpList.getDeptId());
                resultMap.put("sysUser", sysUserWxMpList);
                if (StringUtils.isNotBlank(body.getOpenId()) ) {
                    sysUserWxMpList.setOpenId(body.getOpenId());
                    sysUserService.updateUser(sysUserWxMpList);
                }
            }
        } else if ((StringUtils.isNotBlank(body.getUserName()) && StringUtils.isNotBlank(body.getPassword()))) {
            resultMap.put("userName", body.getUserName());
            resultMap.put("password", body.getPassword());
        } else {
            resultMap = null;
        }
        return resultMap;
    }

    public JSONObject wxGetOpenId(String code) throws WxErrorException {
        JSONObject jsonObject = new JSONObject();

        WxMaJscode2SessionResult jscode2session = WxMaConfiguration.getMaService(WxConfig.AppId).jsCode2SessionInfo(code);
        jsonObject.put("openid", jscode2session.getOpenid());
        return jsonObject;
    }




}
